package com.yilu.driver.ui.activity;


import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.is.common.utils.SPUtils;
import com.is.ui.xrecyclerview.XRecyclerView;
import com.is.ui.xrecyclerview.universaladapter.ViewHolderHelper;
import com.is.ui.xrecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import com.is.ui.xrecyclerview.universaladapter.recyclerview.OnItemClickListener;
import com.yilu.driver.DriverConstant;
import com.yilu.driver.R;
import com.yilu.driver.base.BaseActivity;
import com.yilu.driver.entity.TraineeListResponse;
import com.yilu.driver.mvp.contract.TrainInfoListContract;
import com.yilu.driver.mvp.model.TraineeInfoListModel;
import com.yilu.driver.mvp.presenter.TraineeInfoListPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 学员信息列表
 *
 * @author Administrator
 * @date 2017/04/11
 */
public class TraineeInfoListActivity extends BaseActivity
        implements TrainInfoListContract.View, OnItemClickListener, XRecyclerView.LoadingListener {

    @Bind(R.id.txt_title)
    TextView txtTitle;
    @Bind(R.id.xrc_list)
    XRecyclerView xrcList;
    private TraineeInfoListPresenter traineeInfoListPresenter;
    private TraineeInfoListModel traineeInfoListModel;

    @Bind(R.id.ib_back)
    ImageButton ibBack;
    @Bind(R.id.floatingActionButton)
    FloatingActionButton floatingActionButton;
    private Integer pageNo = 1;
    private Integer pageSize = 10;
    private String applyTypeId;
    private String dotId;
    private String fieldCoachId;
    private String roadCoachId;
    private String entryWayId;
    private CommonRecycleViewAdapter<TraineeListResponse.DataBean> adapter;
    private Integer operator;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_trainee_info_list;
    }

    @Override
    protected void initViewsAndEvents() {
        operator = (Integer) SPUtils.get(mContext, "id", 0);//获取当前登陆人的id
        txtTitle.setText("学员信息");
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrcList.setLayoutManager(layoutManager);
        adapter = new CommonRecycleViewAdapter<TraineeListResponse.DataBean>(mContext, R.layout.item_trainee_info) {
            @Override
            public void convert(ViewHolderHelper helper, TraineeListResponse.DataBean dataBean) {
                TextView txtName = helper.getView(R.id.txt_trainee_name);
                TextView txtArchivesNumber = helper.getView(R.id.txt_trainee_archivesNumber);
                TextView txtSubject = helper.getView(R.id.txt_trainee_subject);
                TextView txtMobile = helper.getView(R.id.txt_trainee_mobile);
                TextView txtPeriods = helper.getView(R.id.txt_trainee_periods);

                txtArchivesNumber.setText("档案号:" + dataBean.getArchivesNumber());
                txtSubject.setText("科" + dataBean.getSubject() + "");
                txtMobile.setText("电话:" + dataBean.getMobile());
                txtName.setText(dataBean.getName());
                txtPeriods.setText("期数:" + dataBean.getPeriods());
            }
        };
        xrcList.setAdapter(adapter);
        xrcList.setLoadingListener(this);//上拉加载下拉刷新
        xrcList.refresh();//自动刷新
        adapter.setOnItemClickListener(this);//item点击
    }
    /**
     * 发送获取学员列表请求
     *
     * @param event_tag    刷新加载标记
     * @param pageNo       页码
     * @param pageSize     每页数量
     * @param operator     操作人
     * @param applyTypeId  车型id
     * @param dotId        报名点id
     * @param fieldCoachId 场训教练id
     * @param roadCoachId  路训教练id
     * @param entryWayId   来源渠道id
     */
    public void getTraineeInfoList(int event_tag, Integer pageNo, Integer pageSize, Integer operator, String applyTypeId
            , String dotId, String fieldCoachId, String roadCoachId, String entryWayId) {
        JSONObject json = new JSONObject();
        try {
            json.put("pageNo", pageNo);
            json.put("pageSize", pageSize);
            json.put("operator", operator);
            json.put("applyTypeId", applyTypeId);
            json.put("dotId", dotId);
            json.put("fieldCoachId", fieldCoachId);
            json.put("roadCoachId", roadCoachId);
            json.put("entryWayId", entryWayId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        traineeInfoListPresenter.getTraineeInfoList(event_tag, json);
    }

    /**
     * 初始化Presenter、Model
     */
    @Override
    public void initPresenter() {
        traineeInfoListModel = new TraineeInfoListModel();
        traineeInfoListPresenter = new TraineeInfoListPresenter(mContext);
        traineeInfoListPresenter.setVM(this, traineeInfoListModel);
    }

    /**
     * 刷新
     *
     * @param traineeListResponse 学员列表项
     */
    @Override
    public void returnTrainListRefresh(TraineeListResponse traineeListResponse) {
        if (adapter!=null) {
            adapter.replaceAll(traineeListResponse.getData());
            adapter.notifyDataSetChanged();
            xrcList.refreshComplete();
        }
    }

    /**
     * 加载更多
     *
     * @param traineeListResponse 学员列表项
     */
    @Override
    public void returnTrainListLoadMore(TraineeListResponse traineeListResponse) {
        if (traineeListResponse != null) {
            adapter.addAll(traineeListResponse.getData());
            xrcList.loadMoreComplete();
            adapter.notifyDataSetChanged();
        } else {
            xrcList.setNoMore(true);
        }
    }

    /**
     * 标题栏点击事件
     *
     * @param view
     */
    @OnClick({R.id.ib_back, R.id.floatingActionButton, R.id.ib_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back://返回上一页
                finish();
                break;
            case R.id.ib_search:
                  //   readyGoForResult(SearchActivity.class, REQUEST_CODE);
                break;
            case R.id.floatingActionButton:
                //        readyGo(AddTraineeActivity.class);
                break;
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, Object o, int position) {
        Bundle bundle = new Bundle();
        bundle.putInt("id", adapter.get(position).getId());
        readyGo(TraineeInfoActivity.class, bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
        return false;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        pageNo = 1;
        getTraineeInfoList(DriverConstant.EVENT_REFRESH, pageNo, pageSize, operator, applyTypeId, dotId, fieldCoachId, roadCoachId, entryWayId);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
        pageNo++;
        getTraineeInfoList(DriverConstant.EVENT_LOADMORE, pageNo, pageSize, operator, applyTypeId, dotId, fieldCoachId, roadCoachId, entryWayId);
    }
}
